package com.villaday.idf.activity;

import android.content.Intent;

import androidx.appcompat.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.model.OrdersPrepare;
import com.villaday.idf.model.PayOrders;
import com.villaday.idf.model.PayOrdersEntity;
import com.villaday.idf.model.Prepare;
import com.villaday.idf.model.RefreshtokenEntity;
import com.villaday.idf.model2.ShippingAddressesEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/*

提交订单
* */
public class CreatOrderActivity extends BaseActivity implements OkHttpUtil.OnDataListener {


    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.sdv)
    SimpleDraweeView sdv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_yuanjia)
    TextView tvYuanjia;
    @BindView(R.id.tv_shoujia)
    TextView tvShoujia;
    @BindView(R.id.tv_youhui)
    TextView tvYouhui;
    @BindView(R.id.tv_useyueBT)
    TextView tvUseyueBT;
    @BindView(R.id.tv_useyue)
    TextView tvUseyue;
    @BindView(R.id.tv_pay_sign)
    TextView tvPaySign;
    @BindView(R.id.tv_pay_num)
    TextView tvPayNum;
    @BindView(R.id.tv_keyong)
    TextView tvKeyong;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    @BindView(R.id.ll_yue)
    LinearLayout llYue;

    @BindView(R.id.ll_youhui)
    LinearLayout llYouhui;


    @BindView(R.id.ll_date)
    LinearLayout llDate;

    @BindView(R.id.ll_shoujia)
    LinearLayout llShoujia;


    @BindView(R.id.ll_canBuyNum)
    LinearLayout llCanBuyNum;
    @BindView(R.id.tv_canBuyNum)
    TextView tvCanBuyNum;
    @BindView(R.id.tv_num)
    TextView tv_num;

    @BindView(R.id.tv_price)
    TextView tvPrice;

    @BindView(R.id.ll_LianxirenNoAddr)
    LinearLayout llLianxirenNoAddr;

    @BindView(R.id.tv_addAddr)
    TextView tvAddAddr;

    @BindView(R.id.ll_addr)
    LinearLayout llAddr;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_addr)
    TextView tvAddr;

    @BindView(R.id.ll_Email)
    LinearLayout llEmail;
    @BindView(R.id.et_Email)
    EditText etEmail;

    @BindView(R.id.ll_IDCard)
    LinearLayout llIDCard;
    @BindView(R.id.et_IDCard)
    EditText etIDCard;

    @BindView(R.id.ll_LianZhuYouhui)
    LinearLayout llLianZhuYouhui;
    @BindView(R.id.tv_LianZhuYouhui)
    TextView tvLianZhuYouhui;

    @BindView(R.id.ll_YaJin)
    LinearLayout llYaJin;
    @BindView(R.id.tv_YaJin)
    TextView tvYaJin;

    @BindView(R.id.textView8)
    TextView textView8;

    @BindView(R.id.ll_yuanjia)
    LinearLayout llYuanjia;

    @BindView(R.id.et_remarks)
    EditText etRemarks;


    @BindView(R.id.ll_datefortemai)
    LinearLayout ll_datefortemai;


    @BindView(R.id.tv_datefortemai)
    TextView tv_datefortemai;


    private Gson gson = new GsonBuilder().create();
    private Prepare.ProductEntity productEntity;
    private Prepare toJson;
    private String in;
    private String out;
    private OrdersPrepare ordersPrepare;
    private LoadingDialog loadingDialog;

    private int buyNum = 1;
    private int loadNum = 0;
    private PayOrders toJson1;
    private PayOrdersEntity payOrdersEntity;
    private PayOrdersEntity payOrdersEntity1;

    private String addressId;//  "addressId": "string,收货地址ID"
    private boolean allowSelectDate;
    private String checkInDate;
    private String checkOutDate;


    @Override
    protected int getViewResId() {
        return R.layout.activity_creat_order;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbarText, "提交订单", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        RefreshtokenEntity.BodyBean roleInfo =
                SharedPreferencesUtil.getRoleInfo(this);
        String roleType = roleInfo.getMember().getType();
        if ("0".equals(roleType)) {
            llYue.setVisibility(View.GONE);
        } else {
            llYue.setVisibility(View.GONE);
            llYue.setVisibility(View.VISIBLE);

        }
        //特卖是否有日期

        allowSelectDate = getIntent().getBooleanExtra("AllowSelectDate", false);
        if (allowSelectDate) {
            ll_datefortemai.setVisibility(View.VISIBLE);
            checkInDate = getIntent().getStringExtra("checkInDate");

            tv_datefortemai.setText(checkInDate);
        } else {
            ll_datefortemai.setVisibility(View.GONE);

        }
        tvKeyong.setVisibility(View.GONE);
    }

    @Override
    protected void loadData() {
        ++loadNum;
        boolean hasHouse = getIntent().getBooleanExtra("hasHouse", true);
        if (hasHouse) {

        } else {//多店通用
            tvDetail.setVisibility(View.GONE);
        }
        if (getIntent().getIntExtra("type", 0) == 0) {//房源类型
            llYuanjia.setVisibility(View.VISIBLE);
        } else {
            llYuanjia.setVisibility(View.GONE);

        }

        loadingDialog = LoadingDialog.newInstance("加载中");
        loadingDialog.show(getFragmentManager(), "loading");
        Map<String, Object> map = new HashMap<>();
        int type = getIntent().getIntExtra("type", 0);
        //map.put("type", type);
        if (type == 0) {//0为普通
            checkInDate = getIntent().getStringExtra("checkInDate");
            checkOutDate = getIntent().getStringExtra("checkOutDate");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat formatNew = new SimpleDateFormat("MM月dd日");

            try {
                in = formatNew.format(format.parse(checkInDate));
                out = formatNew.format(format.parse(checkOutDate));

            } catch (ParseException e) {
                e.printStackTrace();
            }
            tvDate.setText(in + "-" + out + "（" + getIntent().getIntExtra("nightNum", 0) + "晚）");

            textView8.setText("房费");
            llDate.setVisibility(View.VISIBLE);
            productEntity = new Prepare.ProductEntity(getIntent().getStringExtra("product_id"), getIntent().getIntExtra("product_type", 100), getIntent().getIntExtra("product_quantity", buyNum));
            llCanBuyNum.setVisibility(View.GONE);

            // map.put("product", productEntity);

            //map.put("checkInDate", getIntent().getStringExtra("checkInDate"));
            // map.put("checkOutDate", getIntent().getStringExtra("checkOutDate"));
            toJson = new Prepare(type, productEntity, checkInDate, checkOutDate);

        } else if (type == 7) {//7为特卖

            textView8.setText("原价");

            Prepare.ActivityEntity activityEntity = new Prepare.ActivityEntity(getIntent().getStringExtra("activity_id"), getIntent().getIntExtra("activity_type", 13), buyNum);
            // map.put("product", productEntity);

            //map.put("checkInDate", getIntent().getStringExtra("checkInDate"));
            // map.put("checkOutDate", getIntent().getStringExtra("checkOutDate"));
            if (allowSelectDate) {
                toJson = new Prepare(type, activityEntity, checkInDate, checkInDate);

            } else {
                toJson = new Prepare(type, activityEntity);

            }


            llDate.setVisibility(View.GONE);
            tvPrice.setVisibility(View.VISIBLE);

            llCanBuyNum.setVisibility(View.VISIBLE);
            llShoujia.setVisibility(View.GONE);
            tv_num.setText(buyNum + "");

        }

        OkHttpUtil.postJson(this, Constant.URL.orders_prepare, toJson, map, this);
    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.orders_prepare.equals(url)) {
                dismissLoading();

                LogeUtil.e("orders_prepare", json);

                ordersPrepare = gson.fromJson(json, OrdersPrepare.class);
                if ("200".equals(ordersPrepare.getCode())) {
                    if (ordersPrepare.getBody() != null) {
                        initData(ordersPrepare.getBody());
                    }
                } else {
                    ToastUtil.initToast(this, ordersPrepare.getMsg());
                }
            } else if (Constant.URL.orders.equals(url)) {
                dismissLoading();
                LogeUtil.e("orders", json);

                payOrdersEntity1 = gson.fromJson(json, PayOrdersEntity.class);
                if ("200".equals(payOrdersEntity1.getCode())) {
                    if (payOrdersEntity1.getBody() != null) {
                        if (getIntent().getIntExtra("type", 0) == 0) {
                            Intent intent = new Intent(this, PayOrderActivity.class);
                            intent.putExtra("nightNum", getIntent().getIntExtra("nightNum", 0));

                            intent.putExtra("type", getIntent().getIntExtra("type", 0));
                            intent.putExtra("product_id", getIntent().getStringExtra("product_id"));
                            intent.putExtra("product_type", getIntent().getIntExtra("product_type", 100));
                            intent.putExtra("product_quantity", getIntent().getIntExtra("product_quantity", 1));
                            intent.putExtra("checkInDate", checkInDate);
                            intent.putExtra("checkOutDate", checkOutDate);

                            intent.putExtra("mobile", etPhone.getText().toString());
                            intent.putExtra("linkman", etName.getText().toString());
                            intent.putExtra("useWallet", tvUseyueBT.isSelected());
                            intent.putExtra("walletDeductibleAmount", ordersPrepare.getBody().getWalletDeductibleAmount());

                            intent.putExtra("dataEntity", json);


                            intent.putExtra("AllowAddress", ordersPrepare.getBody().isAllowAddress());
                            intent.putExtra("hasHouse", getIntent().getBooleanExtra("hasHouse", true));

                            if (ordersPrepare.getBody().isAllowAddress()) {


                                intent.putExtra("tvName", tvName.getText().toString());
                                intent.putExtra("tvPhone", tvPhone.getText().toString());
                                intent.putExtra("tvAddr", tvAddr.getText().toString());

                            }
                            //备注参数
                            intent.putExtra("remarks", etRemarks.getText().toString());

                            startActivity(intent);
                        } else if (getIntent().getIntExtra("type", 7) == 7) {


                            Intent intent = new Intent(this, PayOrderActivity.class);
                            intent.putExtra("type", getIntent().getIntExtra("type", 7));
                            intent.putExtra("activity_id", getIntent().getStringExtra("activity_id"));
                            intent.putExtra("activity_type", getIntent().getIntExtra("activity_type", 13));

                            intent.putExtra("mobile", etPhone.getText().toString());
                            intent.putExtra("linkman", etName.getText().toString());
                            intent.putExtra("useWallet", tvUseyueBT.isSelected());
                            intent.putExtra("walletDeductibleAmount", ordersPrepare.getBody().getWalletDeductibleAmount());

                            intent.putExtra("quantity", buyNum);

                            intent.putExtra("dataEntity", json);

                            intent.putExtra("AllowAddress", ordersPrepare.getBody().isAllowAddress());

                            intent.putExtra("hasHouse", getIntent().getBooleanExtra("hasHouse", true));
                            if (ordersPrepare.getBody().isAllowAddress()) {


                                intent.putExtra("tvName", tvName.getText().toString());
                                intent.putExtra("tvPhone", tvPhone.getText().toString());
                                intent.putExtra("tvAddr", tvAddr.getText().toString());


                            }
                            intent.putExtra("remarks", etRemarks.getText().toString());
                            if (allowSelectDate) {

                                intent.putExtra("checkInDate", checkInDate);
                                intent.putExtra("allowSelectDate", true);


                            } else {

                            }
                            startActivity(intent);
                        }


                        onBackPressed();
                    }
                } else {
                    ToastUtil.initToast(this, payOrdersEntity1.getMsg());
                }
            }
        }
    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }


    }

    private void initData(OrdersPrepare.BodyEntity data) {
        //是不是 要选地址
        if (data.isAllowAddress()) {
            llLianxirenNoAddr.setVisibility(View.GONE);
            if (data.getAddress() == null) {
                tvAddAddr.setVisibility(View.VISIBLE);
            } else {

                llAddr.setVisibility(View.VISIBLE);
                tvName.setText(data.getAddress().getLinkman());
                tvPhone.setText(data.getAddress().getOverlayMobile());
                tvAddr.setText(data.getAddress().getRegion() + data.getAddress().getAddress());
                addressId = data.getAddress().getId();
            }
        }

        if (data.isAllowEmail()) {
            llEmail.setVisibility(View.VISIBLE);
        } else {
            llEmail.setVisibility(View.GONE);

        }
        if (data.isAllowIdentityNumber()) {
            llIDCard.setVisibility(View.VISIBLE);
        } else {
            llIDCard.setVisibility(View.GONE);

        }
        if (TextUtils.isEmpty(data.getStayDiscountAmountValue())) {
            llLianZhuYouhui.setVisibility(View.GONE);
        } else {
            llLianZhuYouhui.setVisibility(View.VISIBLE);
            tvLianZhuYouhui.setText("-" + data.getStayDiscountAmountValue());
        }
        if ("0".equals(data.getDepositType()) && !TextUtils.isEmpty(data.getDepositAmountValue())) {
            llYaJin.setVisibility(View.VISIBLE);
            tvYaJin.setText("-" + data.getDepositAmountValue());
        } else {
            llYaJin.setVisibility(View.GONE);

        }

        //tvUseyueBT.setSelected(data.isAllowUseWallet());
        if (data.getDiscountAmount() > 0) {
            llYouhui.setVisibility(View.VISIBLE);
        } else {
            llYouhui.setVisibility(View.GONE);
        }

        sdv.setImageURI(data.getProduct().getImageUrl());
        tvTitle.setText(data.getProduct().getName() + "");
        if (data.getProduct().getDesc() == null) {
            tvDetail.setVisibility(View.GONE);
        }
        tvDetail.setText(data.getProduct().getDesc() + "");

        tvYuanjia.setText(Html.fromHtml("" + data.getOriginalPriceValue()));
        tvShoujia.setText(Html.fromHtml("&yen;" + data.getCurrentPriceValue()));
        tvYouhui.setText(Html.fromHtml("-" + data.getDiscountAmountValue()));

        tvKeyong.setText(Html.fromHtml("-" + data.getWalletDeductibleAmountValue()));
        tvUseyue.setText(Html.fromHtml("&yen;" + "当前可用余额" + data.getWalletBalanceValue()));
        tvPaySign.setText(Html.fromHtml("&yen;"));
        tvPayNum.setText(Html.fromHtml(data.getActualPriceValue()));
        tvCanBuyNum.setText("(每人限购" + data.getQuantityUpper() + "份)");
        tvCanBuyNum.setVisibility(data.isHideQuantityUpper() ? View.INVISIBLE : View.VISIBLE);
        tvPrice.setText(Html.fromHtml("&yen;" + data.getProduct().getCurrentPriceValue()));

        if (loadNum == 1) {
            buyNum = data.getQuantityLower();

        }
        tv_num.setText(buyNum + "");
        if (ordersPrepare.getBody().isAllowAddress()) {
            if (TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) {
                //ToastUtil.initToast(this, "请输入邮箱");

            } else if (TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber()) {
                //ToastUtil.initToast(this, "请输入身份证");

            } else {
                tvConfirm.setSelected(true);
            }
        } else {
            if (TextUtils.isEmpty(etName.getText())) {
            } else if (TextUtils.isEmpty(etPhone.getText())) {
                //ToastUtil.initToast(this, "请填写联系人手机号");

            } else if (TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) {
                //ToastUtil.initToast(this, "请输入邮箱");

            } else if (TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber()) {
                //ToastUtil.initToast(this, "请输入身份证");

            } else {
                tvConfirm.setSelected(true);


            }
        }


    }

    @Override
    public void onFailure(String url, String error) {

    }

    //姓名
    @OnTextChanged(value = R.id.et_name, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void etCodeChanged(CharSequence text) {
        if (ordersPrepare != null && ordersPrepare.getBody() != null) {

            if (ordersPrepare.getBody().isAllowAddress()) {
                if ((TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) || TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber()) {
                    tvConfirm.setSelected(false);
                } else {
                    tvConfirm.setSelected(true);

                }
            } else {

                if (TextUtils.isEmpty(etName.getText()) || TextUtils.isEmpty(etPhone.getText()) || (TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) || (TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber())) {

                    tvConfirm.setSelected(false);
                } else {
                    tvConfirm.setSelected(true);

                }
            }


        }
    }

    //手机号
    @OnTextChanged(value = R.id.et_phone, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void etCodeChanged1(CharSequence text) {

        if (ordersPrepare != null && ordersPrepare.getBody() != null) {

            if (ordersPrepare.getBody().isAllowAddress()) {
                if ((TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) || TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber()) {
                    tvConfirm.setSelected(false);
                } else {
                    tvConfirm.setSelected(true);

                }
            } else {

                if (TextUtils.isEmpty(etName.getText()) || TextUtils.isEmpty(etPhone.getText()) || (TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) || (TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber())) {

                    tvConfirm.setSelected(false);
                } else {
                    tvConfirm.setSelected(true);

                }
            }


        }


    }

    //邮箱
    @OnTextChanged(value = R.id.et_Email, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void etCodeChanged2(CharSequence text) {
        if (ordersPrepare != null && ordersPrepare.getBody() != null) {

            if (ordersPrepare.getBody().isAllowAddress()) {
                if ((TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) || TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber()) {
                    tvConfirm.setSelected(false);
                } else {
                    tvConfirm.setSelected(true);

                }
            } else {

                if (TextUtils.isEmpty(etName.getText()) || TextUtils.isEmpty(etPhone.getText()) || (TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) || (TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber())) {

                    tvConfirm.setSelected(false);
                } else {
                    tvConfirm.setSelected(true);

                }
            }


        }
    } //手机号

    @OnTextChanged(value = R.id.et_IDCard, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void etCodeChanged3(CharSequence text) {
        if (ordersPrepare != null && ordersPrepare.getBody() != null) {

            if (ordersPrepare.getBody().isAllowAddress()) {
                if ((TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) || TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber()) {
                    tvConfirm.setSelected(false);
                } else {
                    tvConfirm.setSelected(true);

                }
            } else {

                if (TextUtils.isEmpty(etName.getText()) || TextUtils.isEmpty(etPhone.getText()) || (TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) || (TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber())) {

                    tvConfirm.setSelected(false);
                } else {
                    tvConfirm.setSelected(true);

                }
            }


        }
    }

    @OnClick({R.id.tv_datefortemai, R.id.tv_date, R.id.ll_addr, R.id.tv_addAddr, R.id.tv_useyueBT, R.id.tv_confirm, R.id.img_minus, R.id.img_plus})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_datefortemai://特卖房源选择日期

                Intent intent3 = new Intent(this, ChouseCheckInDateActivityForTeMai.class);

                intent3.putExtra("type", 7);
                intent3.putExtra("activity_id", getIntent().getStringExtra("activity_id"));
                intent3.putExtra("activity_type", 13);
                intent3.putExtra("hasHouse", getIntent().getStringExtra("hasHouse"));
                intent3.putExtra("isNextChoose", true);

                //intent3.putExtra("hasHouse", mYHousesEntity.getBody().is);


                startActivityForResult(intent3, 405);


                break;
            case R.id.tv_date://普通房源选择日期

                Intent intent1 = new Intent(this, ChouseCheckInDateActivity.class);
                intent1.putExtra("isNextChoose", true);
                //intent3.putExtra("hasHouse", mYHousesEntity.getBody().is);
                intent1.putExtra("houseId", getIntent().getStringExtra("houseId"));
                intent1.putExtra("type", 0);
                intent1.putExtra("product_id", getIntent().getStringExtra("product_id"));
                intent1.putExtra("product_type", 100);
                intent1.putExtra("product_quantity", 1);

                startActivityForResult(intent1, 403);

                break;
            case R.id.tv_addAddr: {//添加地址
                Intent intent = new Intent(this, AddrManagerActivity.class);
                intent.putExtra("needfinnish", true);

                startActivityForResult(intent, 666);


            }
            break;
            case R.id.ll_addr: {//添加地址
                Intent intent = new Intent(this, AddrManagerActivity.class);
                intent.putExtra("needfinnish", true);

                startActivityForResult(intent, 666);
            }
            break;
            case R.id.tv_useyueBT://使用余额
                if (ordersPrepare != null && ordersPrepare.getBody() != null) {
                    if (ordersPrepare.getBody().getWalletDeductibleAmount() > 0) {
                        tvUseyueBT.setSelected(!tvUseyueBT.isSelected());
                        if (tvUseyueBT.isSelected()) {
                            tvKeyong.setVisibility(View.VISIBLE);

                            tvPayNum.setText(Html.fromHtml(PhoneUtil.handleBalance((ordersPrepare.getBody().getActualPrice() - ordersPrepare.getBody().getWalletDeductibleAmount() > 0 ? ordersPrepare.getBody().getActualPrice() - ordersPrepare.getBody().getWalletDeductibleAmount() : 0) / 100.0 + "", 2)));

                        } else {
                            tvKeyong.setVisibility(View.GONE);

                            tvPayNum.setText(Html.fromHtml(ordersPrepare.getBody().getActualPriceValue()));

                        }
                    }
                }


                break;
            case R.id.tv_confirm://支付


                if (ordersPrepare != null && ordersPrepare.getBody() != null) {

                    if (ordersPrepare.getBody().isAllowAddress()) {
                        if (TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) {
                            //ToastUtil.initToast(this, "请输入邮箱");

                        } else if (TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber()) {
                            //ToastUtil.initToast(this, "请输入身份证");

                        } else {
                            pay();
                        }
                    } else {
                        if (TextUtils.isEmpty(etName.getText())) {
                            //ToastUtil.initToast(this, "请填写联系人姓名");
                        } else if (TextUtils.isEmpty(etPhone.getText())) {
                            //ToastUtil.initToast(this, "请填写联系人手机号");

                        } else if (TextUtils.isEmpty(etEmail.getText()) && ordersPrepare.getBody().isAllowEmail()) {
                            //ToastUtil.initToast(this, "请输入邮箱");

                        } else if (TextUtils.isEmpty(etIDCard.getText()) && ordersPrepare.getBody().isAllowIdentityNumber()) {
                            //ToastUtil.initToast(this, "请输入身份证");

                        } else {
                            pay();
                        /*if (getIntent().getIntExtra("type", 0) == 0) {
                            Intent intent = new Intent(this, PayOrderActivity.class);
                            intent.putExtra("type", getIntent().getIntExtra("type", 0));
                            intent.putExtra("product_id", getIntent().getStringExtra("product_id"));
                            intent.putExtra("product_type", getIntent().getIntExtra("product_type", 100));
                            intent.putExtra("product_quantity", getIntent().getIntExtra("product_quantity", 1));
                            intent.putExtra("checkInDate", getIntent().getStringExtra("checkInDate"));
                            intent.putExtra("checkOutDate", getIntent().getStringExtra("checkOutDate"));

                            intent.putExtra("mobile", etPhone.getText().toString());
                            intent.putExtra("linkman", etName.getText().toString());
                            intent.putExtra("useWallet", tvUseyueBT.isSelected());
                            intent.putExtra("walletDeductibleAmount", ordersPrepare.getBody().getWalletDeductibleAmount());

                            startActivity(intent);
                        } else if (getIntent().getIntExtra("type", 7) == 7) {


                            Intent intent = new Intent(this, PayOrderActivity.class);
                            intent.putExtra("type", getIntent().getIntExtra("type", 7));
                            intent.putExtra("activity_id", getIntent().getStringExtra("activity_id"));
                            intent.putExtra("activity_type", getIntent().getIntExtra("activity_type", 13));

                            intent.putExtra("mobile", etPhone.getText().toString());
                            intent.putExtra("linkman", etName.getText().toString());
                            intent.putExtra("useWallet", tvUseyueBT.isSelected());
                            intent.putExtra("walletDeductibleAmount", ordersPrepare.getBody().getWalletDeductibleAmount());

                            intent.putExtra("quantity", buyNum);

                            startActivity(intent);
                        }


                        onBackPressed();*/


                        }
                    }


                }

                break;
            case R.id.img_minus: {//减

                if (buyNum != 1) {
                    buyNum--;
                    tv_num.setText(buyNum + "");
                    loadData();

                }

            }
            break;
            case R.id.img_plus: {//加
                if (buyNum != ordersPrepare.getBody().getQuantityUpper()) {
                    buyNum++;
                    tv_num.setText(buyNum + "");
                    loadData();
                }
            }
            break;
        }
    }

    private void pay() {
        loadingDialog = LoadingDialog.newInstance("加载中");
        loadingDialog.show(getFragmentManager(), "loading");
        Map<String, Object> map = new HashMap<>();
        int type = getIntent().getIntExtra("type", 0);
        //map.put("type", type);
        if (type == 0) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat formatNew = new SimpleDateFormat("MM月dd日");
            try {
                in = formatNew.format(format.parse(checkInDate));
                out = formatNew.format(format.parse(checkOutDate));

            } catch (ParseException e) {
                e.printStackTrace();
            }
            tvDate.setText(in + "-" + out);
            llDate.setVisibility(View.VISIBLE);
            PayOrders.ProductEntity productEntity1 = new PayOrders.ProductEntity(getIntent().getStringExtra("product_id"), getIntent().getIntExtra("product_type", 100), getIntent().getIntExtra("product_quantity", 1));
            // map.put("product", productEntity);

            //map.put("checkInDate", getIntent().getStringExtra("checkInDate"));
            // map.put("checkOutDate", getIntent().getStringExtra("checkOutDate"));

            /*intent.putExtra("type", getIntent().getIntExtra("type", 0));
            intent.putExtra("product_id", getIntent().getStringExtra("product_id"));
            intent.putExtra("product_type", getIntent().getIntExtra("product_type", 100));
            intent.putExtra("product_quantity", getIntent().getIntExtra("product_quantity", 1));
            intent.putExtra("checkInDate", getIntent().getStringExtra("checkInDate"));
            intent.putExtra("checkOutDate", getIntent().getStringExtra("checkOutDate"));

            intent.putExtra("mobile", etPhone.getText());
            intent.putExtra("linkman", etName.getText());
            intent.putExtra("useWallet", ordersPrepare.getBody().isAllowUseWallet());
            intent.putExtra("walletDeductibleAmount", ordersPrepare.getBody().getWalletDeductibleAmount());*/


            toJson1 = new PayOrders(type, productEntity1, checkInDate, checkOutDate, etPhone.getText().toString(), etName.getText().toString(), tvUseyueBT.isSelected(), ordersPrepare.getBody().getWalletDeductibleAmount(), addressId, etEmail.getText().toString(), etIDCard.getText().toString(), etRemarks.getText().toString());

        } else if (type == 7) {
            PayOrders.ActivityEntity activityEntity1 = new PayOrders.ActivityEntity(getIntent().getStringExtra("activity_id"), getIntent().getIntExtra("activity_type", 13), buyNum);
            toJson1 = new PayOrders(type, activityEntity1, etPhone.getText().toString(), etName.getText().toString(), tvUseyueBT.isSelected(), ordersPrepare.getBody().getWalletDeductibleAmount(), addressId, etEmail.getText().toString(), etIDCard.getText().toString(), etRemarks.getText().toString());
            if (allowSelectDate) {


                toJson1 = new PayOrders(type, activityEntity1, etPhone.getText().toString(), etName.getText().toString(), tvUseyueBT.isSelected(), ordersPrepare.getBody().getWalletDeductibleAmount(), addressId, etEmail.getText().toString(), etIDCard.getText().toString(), etRemarks.getText().toString(), checkInDate, checkInDate);

            } else {
                toJson1 = new PayOrders(type, activityEntity1, etPhone.getText().toString(), etName.getText().toString(), tvUseyueBT.isSelected(), ordersPrepare.getBody().getWalletDeductibleAmount(), addressId, etEmail.getText().toString(), etIDCard.getText().toString(), etRemarks.getText().toString());

            }

        }

        OkHttpUtil.postJson(this, Constant.URL.orders, toJson1, map, this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 666) {
                tvAddAddr.setVisibility(View.GONE);

                llAddr.setVisibility(View.VISIBLE);

                //Intent entity = getIntent().putExtra("entity", shippingAddressesEntity.getBody().get(position));
                ShippingAddressesEntity.BodyEntity entity = data.getParcelableExtra("entity");
                tvName.setText(entity.getLinkman());
                tvPhone.setText(entity.getOverlayMobile());
                tvAddr.setText(entity.getRegion() + entity.getAddress());
                addressId = entity.getId();
            } else if (requestCode == 403) {//普通


                checkInDate = data.getStringExtra("checkInDate");

                checkOutDate = data.getStringExtra("checkOutDate");

                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat formatNew = new SimpleDateFormat("MM月dd日");

                try {
                    in = formatNew.format(format.parse(checkInDate));
                    out = formatNew.format(format.parse(checkOutDate));

                } catch (ParseException e) {
                    e.printStackTrace();
                }
                tvDate.setText(in + "-" + out + "（" + getIntent().getIntExtra("nightNum", 0) + "晚）");

            } else if (requestCode == 405) {//特卖
                checkInDate = data.getStringExtra("checkInDate");

                tv_datefortemai.setText(checkInDate);
            }
        }
    }
}
